from django.db import models


class Post(models.Model):
    sort = models.CharField(max_length=10)
    unit = models.CharField(max_length=10)
    price = models.CharField(max_length=10)
    name = models.CharField(max_length=10,default='')
    counts = models.IntegerField(default=0)
    subtotal = models.CharField(max_length=20,default='')
    send = models.IntegerField(default=0)
    offer = models.IntegerField(default=0)
    @classmethod
    def get_sum(cls):
        number =0;
        for item in Post.objects.all():
            number+=item.counts;
        return number;

    @classmethod
    def get_total(cls):
        total = 0;
        for i in Post.objects.all():
            total += float(i.price) * int(i.counts)
        return total;

    @classmethod
    def get_saving(cls):
        save = 0 ;
        for item in Post.objects.all():
             save += float(item.price) * int(item.send)
        return save;

    @classmethod
    def get_Subtotal(cls):
        for item in Post.objects.all():
            if (item.counts >= 3 and item.offer == 1):
                item.send = int(item.counts / 3)
                total = float(item.price) * (int(item.counts-item.send))
                original = float(item.price) * (int(item.counts));
                item.subtotal = str(total) + '元' + '(' + '原价' + '\n' + str(original) + '元' + ')'
                item.save();
            elif (item.counts > 0):
                item.send = 0;
                original = float(item.price) * (int(item.counts));
                item.subtotal = str(original) + '元';
                item.save();
        return Post;